#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @auth:  spacelacc
# @date:  2025/04/13
# @func:  widget dialog to import data
# @vers:  V1

from PySide6.QtCore    import Signal
from PySide6.QtWidgets import QDialog, QWidget
from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout
from PySide6.QtWidgets import QStackedWidget, QPushButton

from customtableview   import CustomTableView
from customfilebrowser import CustomFileBrowser


class CopyFileDialog(QDialog):
    def donothing(self):
        pass

    def __init__(self, width, height, handler):
        super().__init__()

        # file browser
        self.chooser = QWidget()
        self.browser = CustomFileBrowser()
        self.fileconfirm = QPushButton('Confirm')
        self.filecancel  = QPushButton('Cancel')
        ctfileact = QHBoxLayout()
        ctfileact.addStretch(1)
        ctfileact.addWidget(self.fileconfirm)
        ctfileact.addWidget(self.filecancel)
        ctchooser = QVBoxLayout(self.chooser)
        ctchooser.addWidget(self.browser)
        ctchooser.addLayout(ctfileact)

        # data preview
        self.preview = QWidget()
        self.slider  = CustomTableView()
        self.viewretreat = QPushButton('Go Back')
        self.viewconfirm = QPushButton('Confirm')
        self.viewcancel  = QPushButton('Cancel')
        ctviewact = QHBoxLayout()
        ctviewact.addWidget(self.viewretreat)
        ctviewact.addStretch(1)
        ctviewact.addWidget(self.viewconfirm)
        ctviewact.addWidget(self.viewcancel)
        ctpreview = QVBoxLayout(self.preview)
        ctpreview.addWidget(self.slider)
        ctpreview.addLayout(ctviewact)

        self.switcher = QStackedWidget()
        self.switcher.addWidget(self.chooser)
        self.switcher.addWidget(self.preview)
        ctdialog = QVBoxLayout(self)
        ctdialog.addWidget(self.switcher)
        self.setLayout(ctdialog)
        self.resize(width, height)

        self.fileconfirm.clicked.connect(self.confirmFileParsing)
        self.filecancel.clicked.connect(self.cancelFileParsing)
        self.viewretreat.clicked.connect(self.retreatDataPreview)
        self.viewconfirm.clicked.connect(self.confirmDataPreview)
        self.viewcancel.clicked.connect(self.cancelDataPreview)
        
    def confirmFileParsing(self):
        self.filebuffers.clear()
        files = self.browser.getSelected()
        for file in files:
            print(file)
            if file.endswith('.xls'):
                self.filebuffers.append(file)
        self.crawler.parse(self.filebuffers)
        self.slider.renewTableView(self.crawler.getparseddata())
